
<body class="iframe">
    <div class="list">
        <div class="frame-header">
            <h2><i class="layui-icon">&#xe705;</i>模板管理</h2>
        </div>
        <h4 class="layui-bg-dark frame-title"><i class="layui-icon">&#xe65f;</i>模板列表</h4>
        <div class="toolbox layui-form">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-inline" style="width: 80px;">
                        <input type="checkbox" title="全选" lay-skin="primary" lay-filter="checkall" class="checkall">
                    </div>
                    <div class="layui-input-inline" style="width: 100px;">
                        <button class="layui-btn layui-btn-danger delall"><i class="layui-icon">&#xe640;</i>删除</button>
                    </div>
                </div>
            </div>
        </div>
        <div class="data-table">
            <table class="layui-table">
                <thead>
                    <tr>
                        <th></th>
                        <th>id</th>
                        <th>邮件主题</th>
                        <th>邮件内容</th>
                        <th>防监测</th>
                        <th>附件</th>
                        <th>添加时间</th>
                        <th>修改时间</th>
                        <th>模板简介</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
        <div id="template-page" class="page"></div>
    </div>
    <form id="update-template" class="layui-form">
        <input type="hidden" id="id-input" name="id"/>
        <div class="layui-form-item">
            <label class="layui-form-label">模板简介</label>
            <div class="layui-input-inline">
                <input type="text" name="brief" required lay-verify="required" placeholder="请输入简介" autocomplete="off" class="layui-input brief">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-inline">
                <input type="text" name="sort" required lay-verify="required|number"  autocomplete="off" class="layui-input sort" value="0">
            </div>
            <div class="layui-form-mid layui-word-aux">值越大,越靠前(0-99)</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮件主题</label>
            <div class="layui-input-inline">
                <input type="text" name="subject" required lay-verify="required" placeholder="请输入主题" autocomplete="off" class="layui-input subject">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">附件地址</label>
            <div class="layui-input-inline">
                <input type="text" autocomplete="off" class="layui-input localurl">
                <input type="hidden" name="attachurl" class="serverurl"></input>
            </div>
            <button type="button" class="layui-btn" id="uploadattachment"><i class="layui-icon">&#xe67c;</i>上传附件</button>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">邮件正文</label>
            <div class="layui-input-block">
                <script id="editor" type="text/plain" style="width:800px;height:500px;" name="content"></script>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="updatetemplate">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</body>
<script type="text/javascript" charset="utf-8" src="<?=base_url()?>public/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="<?=base_url()?>public/ueditor/ueditor.all.min.js"> </script>
<script type="text/javascript" charset="utf-8" src="<?=base_url()?>public/ueditor/lang/zh-cn/zh-cn.js"></script>
<script>
    var ue = UE.getEditor('editor');
    var startpage = 0, len = 10;
    layui.use(['form', 'laypage', 'upload'], function () {



        var form = layui.form, laypage = layui.laypage, upload = layui.upload;
        getTemplateList(startpage, len);

        laypage.render({
            elem: 'template-page'
            , count: "<?= $sumRecords ?>" //数据总数，从服务端得到
            , theme: '#01AAED'
            , limit: len
            , jump: function (obj, first) {
                page = obj.curr - 1;
                len = obj.limit;
                if (!first) {
                    startpage = page;
                    getTemplateList(startpage, len);
                }
            }
        });



        //全选特效
        form.on('checkbox(checkall)', function (data) {
            let checkstate = data.elem.checked;//全选状态
            $("td input[type=checkbox]").prop('checked', checkstate);
        });

        $(document).on('click', 'td input[type=checkbox]', function () {
            let len = $("td input[type=checkbox]").length;
            let clen = 0;
            $("td input[type=checkbox]:checked").each(function () {
                clen++;
            });
            let state = len == clen ? true : false;
            if (state) {
                $(".checkall").prop('checked', true);
                $(".layui-unselect").addClass("layui-form-checked");
            } else {
                $(".checkall").prop('checked', false);
                $(".layui-unselect").removeClass("layui-form-checked");
            }
        })
        //全选删除
        $(".delall").on('click', function () {
            //被选中的元素集合
            let url = '<?= site_url("template/del_template_list") ?>';
            let ids = getCheckedItems();
            let jsonData = {
                'ids': ids
            }
            ids.length == 0 ? layer.msg('请选择需要删除项!') : paramsRequest(url, jsonData, layer, getTemplateList, page, len);
        })

        //修改防检测
        $(document).on('click', '.ismessy', function () {
            let url = '<?= site_url("template/change_template_messy") ?>';
            let id = getId($(this));
            let messyid = $(this).attr('messyid');
            messyid == 0 ? messyid = 1 : messyid = 0;
            let jsonData = {
                'id': id,
                'ismessy': messyid
            };
            paramsRequest(url, jsonData, layer, getTemplateList, page, len);
        });
        //单个删除
        $(document).on('click', '.del', function () {
            let url = '<?= site_url("template/del_template_list") ?>';
            let ids = getId($(this));
            let jsonData = {
                'ids': ids
            };
            paramsRequest(url, jsonData, layer, getTemplateList, page, len);
        });

        //编辑添加弹出层数据
        $(document).on('click', '.edit', function () {
            let id = getId($(this));
            let subject = $(this).parents('tr').find('.subject').text();
            let content = $(this).parents('tr').find('.content input').val();
            let brief = $(this).parents('tr').find('.brief').text();
            let sort = $(this).parents('tr').find('input.sort').val();
            let attach = $(this).parents('tr').find('a').attr('href');
            if (attach == '') {
                attach = '还没有附件,点击上传->';
            }
            $("#update-template #id-input").val(id);
            $("#update-template .subject").val(subject);
            $("#update-template .content").val(content);
            $("#update-template .brief").val(brief);
            $("#update-template .sort").val(sort);
            $("#update-template .localurl").val(attach);
            let myindex = layer.open({
                title: '编辑模板',
                type: 1,
                area: ['500px', '500px'],
                content: $("#update-template"),
                maxmin: true,
                success: function () {
                    ue.addListener("ready", function () {
                        ue.setContent(content);
                    });
                }
            })
            layer.full(myindex);

        });
        form.on('submit(updatetemplate)', function (data) {
            let url = "<?= site_url('template/update_template') ?>";
            let id = data.field.id;//id
            let subject = data.field.subject;//主题
            let content = data.field.content;//内容
            let brief = data.field.brief;//简介
            let sort = data.field.sort;//排序
            let attachment = data.field.attachurl;//附件
            let jsonData = {
                'id': id,
                'subject': subject,
                'content': content,
                'brief': brief,
                'sort': sort,
                'attachment': attachment
            };
            paramsRequest(url, jsonData, layer, getTemplateList, page, len);
            return false;
        });

        //上传文件
        var uploadInst = upload.render({
            elem: '#uploadattachment',
            accept: 'file',
            size: 8192, //最大支持8m的文件上传
            exts: 'png|gif|jpg|jpeg|txt|xls|mp4|wav',
            auto: false,
            url: '<?= site_url("upload/do_upload") ?>',
            choose: function (obj) {
                layer.confirm('确定上传此附件?', function (index) {
                    obj.preview(function (index, file, result) {
                        $(".localurl").val(file.name);
                        layer.load();
                        obj.upload(index, file);
                    });
                });
            },
            done: function (redata) {
                if (redata.errno == '666') {
                    layer.closeAll('loading');
                    layer.msg(redata.error);
                    $(".serverurl").val(redata.uploadInfo.upload_data.file_name);
                } else {
                    layer.msg(redata.error);
                }
            },
            error: function () {
                layer.msg('上传异常');
            }
        });



    });
</script>
<script>
    function getTemplateList(page, limit) {
        let url = '<?= site_url("template/get_template_list") ?>';
        let jsonData = {
            'page': page,
            'limit': limit
        };
        $.post(url, jsonData, function (redata) {
            let str = '';
            for (x in redata.templateList) {
                let ismessy = '已关闭';
                let ismessystyle = 'layui-btn-primary';
                let messyid = 0;
                let attach = '没有';
                let attachstyle = 'layui-btn-disabled';
                let attachurl = '';
                if (redata.templateList[x].is_messy == 1) {
                    messyid = 1;
                    ismessy = '已开启';
                    ismessystyle = 'layui-btn-warm';
                }
                if (redata.templateList[x].attachment.length > 10) {
                    attach = '查看';
                    attachstyle = '';
                    attachurl = '<?= base_url() ?>' + redata.templateList[x].attachment;
                }
                let num = parseInt(x) + 1 + startpage * len;
                str += "<tr tid='" + redata.templateList[x].id + "'><input type='hidden' class='sort' value='" + redata.templateList[x].sort + "'><td><input type='checkbox'  /></td><td>" + num + "</td><td class='subject'>" + redata.templateList[x].subject + "</td>\n\
                <td><button class='layui-btn layui-btn-sm layui-btn-primary content'><i class='layui-icon'>&#xe615;</i><input type='hidden' value='" + redata.templateList[x].content + "'/></button></td><td><button class='layui-btn  " + ismessystyle + " layui-btn-xs ismessy' messyid='" + messyid + "'>" + ismessy + "</button></td><td><a class='layui-btn layui-btn-xs " + attachstyle + " attach' href='" + attachurl + "' target='top'>" + attach + "</a></td><td>" + redata.templateList[x].create_time + "</td><td>" + redata.templateList[x].update_time + "</td><td class='brief'>" + redata.templateList[x].brief + "</td><td><div class='layui-btn-group'><button class='layui-btn layui-btn-normal layui-btn-xs edit'><i class='layui-icon'>&#xe642;</i>编辑</button><button class='layui-btn layui-btn-danger layui-btn-xs del'><i class='layui-icon'>&#xe640;</i>删除</button></div></td></tr>";
            }
            $(".data-table tbody").html(str);
        }, 'json');
    }




</script>
<script>
    $(function () {
        $(document).on('click', '.content', function () {
            let htmlstr = $(this).find("input").val();
            layer.tips(htmlstr, $(this),{
         time: 5000,
         area: ['750px']
    });
        })
    })
</script>
</html>